package vip.rssc.jdbc.mapper.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import vip.rssc.jdbc.entity.TestUser;
import vip.rssc.jdbc.mapper.TestUserMapper;

import java.util.List;

/**
 * <p>Description: [TestUserMapperImpl]</p >
 * Created on 2021年10月20日
 *
 * @author <a href="chenjia@fehorizon.com">CJ</a>
 * @version 1.0
 * Copyright (c) 2021 上海宏信建设发展有限公司
 */
@Repository
public class TestUserMapperImpl implements TestUserMapper {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void save(TestUser testUser) {
        String sql = "insert into test_user(name,age,birthday,remark) values (?,?,?,?)";
        Object[] objects = {testUser.getName(), testUser.getAge(), testUser.getBirthday(), testUser.getRemark()};
        jdbcTemplate.update(sql, objects);
    }

    @Override
    public void updateById(TestUser testUser) {
        String sql = "update test_user set name=?,age=?,birthday=?,remark=? where id=?";
        Object[] objects = {testUser.getName(), testUser.getAge(), testUser.getBirthday(), testUser.getRemark(), testUser.getId()};
        jdbcTemplate.update(sql, objects);
    }

    @Override
    public void deleteById(Long id) {
        String sql = "delete from test_user where id=?";
        Object[] objects = {id};
        jdbcTemplate.update(sql, objects);
    }

    @Override
    public TestUser selectById(Long id) {
        String sql = "select * from test_user where id = ?";
        RowMapper<TestUser> rowMapper = new BeanPropertyRowMapper<>(TestUser.class);
        TestUser testUser = jdbcTemplate.queryForObject(sql, rowMapper, id);
        return testUser;
    }

    @Override
    public List<TestUser> selectList() {
        String sql = "select * from test_user";
        RowMapper<TestUser> rowMapper = new BeanPropertyRowMapper<>(TestUser.class);
        List<TestUser> data = jdbcTemplate.query(sql, rowMapper);
        return data;
    }
}
